package com.fortune.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.ReturnDepositMgtExcution;
import com.fortune.dto.in.returnDepositMgt.ReturnDepositMgtListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.ReturnDepositMgtEnum;
import com.fortune.service.ReturnDepositMgtService;
import com.fortune.vo.returnDepositMgt.ReturnDepositMgt;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "returnDepositMgt", description = "退押金工单处理", tags = {"退押金工单处理"})
@RestController
@RequestMapping("returnDepositMgt")
public class ReturnDepositMgtController {
	
	@Autowired
	private ReturnDepositMgtService rs;
	
	@ApiOperation(value = "工单列表", nickname = "returnDepositMgtList", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<ReturnDepositMgt>> list(HttpServletRequest request,@ApiIgnore @Validated ReturnDepositMgtListDto listDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		listDto.setPage((listDto.getPage()-1)*10);
		PagingData<ReturnDepositMgt> page= rs.page(dUserInfo, listDto);
		return new ReturnDepositMgtExcution<PagingData<ReturnDepositMgt>>(page, ReturnDepositMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "批准退款", nickname = "returnDepositAllow", notes = "", tags = {"@岳希伟"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="工单id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string")
	})
    @PutMapping(value = "allow",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> allow(HttpServletRequest request,@Validated @NotBlank(message="工单id不能为空") String id,
			@Validated @NotBlank(message="客户id不能为空") String clientId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int result = rs.allow(dUserInfo, id, clientId);
		if (result==0) {
			return new ReturnDepositMgtExcution<Void>(null, ReturnDepositMgtEnum.FAILURE);
		}else if (result==1) {
			return new ReturnDepositMgtExcution<Void>(null, ReturnDepositMgtEnum.SUCCESS);
		}else {
			return new ReturnDepositMgtExcution<Void>(null, ReturnDepositMgtEnum.NOCHECK);
		}
	}
	
	@ApiOperation(value = "分派司机", nickname = "assignDriver", notes = "", tags = {"@岳希伟"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="工单id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="driverId", value="司机id",paramType="query",dataType="string")
	})
    @PutMapping(value = "assignDriver",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> assignDriver(HttpServletRequest request,@Validated @NotBlank(message="工单id不能为空") String id,
			@Validated @NotBlank(message="客户id不能为空") String clientId,
			@Validated @NotBlank(message="司机id不能为空") String driverId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int result = rs.assignDriver(dUserInfo, id, clientId, driverId);
		if (result==0) {
			return new ReturnDepositMgtExcution<Void>(null, ReturnDepositMgtEnum.FAILURE);
		}else if (result==1) {
			return new ReturnDepositMgtExcution<Void>(null, ReturnDepositMgtEnum.SUCCESS);
		}else {
			return new ReturnDepositMgtExcution<Void>(null, ReturnDepositMgtEnum.NOCHECK);
		}
	}
}
